package com.xh.hongaiagent.rag;


import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.rag.Query;
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer;
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 查询重写器
 */
@Configuration
public class QueryRewriter {

    private  final QueryTransformer queryTransformer;

    public QueryRewriter(ChatModel dashscopeModel) {
        ChatClient.Builder builder = ChatClient.builder(dashscopeModel);
        queryTransformer = RewriteQueryTransformer.builder()
                .chatClientBuilder(builder)
                .build();
    }

    /**
     * 查询重写
     * @param prompt
     * @return
     */
    public String rewriteQuery(String prompt) {
        Query query = new Query(prompt);
        return queryTransformer.transform(query).text();
    }
}
